﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class MaterialSearchHander : IRequestHandler<MaterialSearchCommand, ResultPage<MaterialListDto>>
    {

        private readonly IBaseRepositorys<Product> productRepository;
        private readonly IBaseRepositorys<Material> materialRepository;

        public MaterialSearchHander(IBaseRepositorys<Product> productRepository, IBaseRepositorys<Material> materialRepository)
        {
            this.productRepository = productRepository;
            this.materialRepository = materialRepository;
        }

        /// <summary>
        /// 物料信息查询显示
        /// </summary>
        /// <param name="request">物料信息查询显示请求对象</param>
        /// <param name="cancellationToken">允许取消异步</param>
        /// <returns>物料信息查询显示响应对象</returns>
        public async Task<ResultPage<MaterialListDto>> Handle(MaterialSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultPage<MaterialListDto>();

                var list1 = (from a in materialRepository.GetAll().Where(x => x.ParentId == 0 && !x.DeleteDate)
                            join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                            select new
                            {
                                a.ProductId,
                                a.MaterialId,
                                a.UnitMach,
                                a.ParentId,
                                a.CreateId,
                                a.CreateDate,
                                a.CreateName,
                                a.UpdateId,
                                a.UpdateName,
                                a.UpdateDate,
                                b.ProductNumber,
                                b.ProductName,
                                b.ProductSpecification,
                                b.StockUnit,
                                ParentName = ""
                            }).ToList();


                var result1 = list1.Select(c => new MaterialListDto
                {
                    ProductNumber = c.ProductNumber,
                    ProductName = c.ProductName,
                    ProductSpecification = c.ProductSpecification,
                    StockUnit = c.StockUnit,
                    UnitMach = c.UnitMach,
                    ParentId = c.ParentId,
                    CreateId = c.CreateId,
                    CreateDate = Convert.ToDateTime(  c.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")),
                    CreateName = c.CreateName,
                    UpdateId = c.UpdateId,
                    UpdateName = c.UpdateName,
                    UpdateDate = c.UpdateDate,
                    MaterialId = c.MaterialId,
                    ParentName = c.ParentName,
                    UnitName = c.StockUnit == 1 ? "条" : (c.StockUnit == 2 ? "张" : (c.StockUnit == 3 ? "车" : (c.StockUnit == 4 ? "车" : (c.StockUnit == 5 ? "天" : (c.StockUnit == 6 ? "小时" : (c.StockUnit == 7 ? "分钟" : (c.StockUnit == 8 ? "周" : (c.StockUnit == 9 ? "月" : "")))))))),
                    Children = GetMaterialListDtos(c.MaterialId).Count() > 0 ? GetMaterialListDtos(c.MaterialId) : null
                }).Distinct().ToList();





                int pid = 0;
                if(!string.IsNullOrEmpty(request.ProductNumber))
                {
                    result1 = result1.Where(x => x.ProductNumber.Contains(request.ProductNumber)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductName))
                {
                    result1 = result1.Where(x => x.ProductName.Contains(request.ProductName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductSpecification))
                {
                    result1 = result1.Where(x => x.ProductSpecification.Contains(request.ProductSpecification)).ToList();
                }
                result.totalCount = result1.Count();
                result.totalPage = (int)Math.Ceiling(result.totalCount / (double)request.PageSize);
                result.Data = result1.OrderBy(c=>c.MaterialId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                return result;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 递归获取物料列表
        /// </summary>
        /// <param name="Pid">父级id</param>
        /// <returns>返回树形物料信息</returns>
        private List<MaterialListDto> GetMaterialListDtos(int Pid )
        {
            var list = (from a in materialRepository.GetAll().Where(x => x.ParentId == Pid && !x.DeleteDate)
                        join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                        join c in materialRepository.GetAll() on a.ParentId equals c.MaterialId 
                        join d in productRepository.GetAll() on c.ProductId equals d.ProductId
                        select new
                        {
                            a.ProductId,
                            a.MaterialId,
                            a.UnitMach,
                            a.ParentId,
                            a.CreateId,
                            a.CreateDate,
                            a.CreateName,
                            a.UpdateId,
                            a.UpdateName,
                            a.UpdateDate,
                            b.ProductNumber,
                            b.ProductName,
                            b.ProductSpecification,
                            b.StockUnit,
                           ParentNames = d.ProductName
                        }).ToList();


            var result = list.Select(c=>new MaterialListDto
            {
                ProductNumber = c.ProductNumber,
                ProductName = c.ProductName,
                ProductSpecification = c.ProductSpecification,
                StockUnit = c.StockUnit,
                UnitMach = c.UnitMach,
                ParentId = c.ParentId,
                CreateId = c.CreateId,
                CreateDate = c.CreateDate,
                CreateName = c.CreateName,
                UpdateId = c.UpdateId,
                UpdateName = c.UpdateName,
                UpdateDate = c.UpdateDate,
                MaterialId = c.MaterialId,
                ParentName = c.ParentNames,
                UnitName =  c.StockUnit == 1 ? "条" : (c.StockUnit == 2 ? "张" : (c.StockUnit == 3 ? "车" : (c.StockUnit == 4 ? "车" : (c.StockUnit == 5 ? "天" : (c.StockUnit == 6 ? "小时" : (c.StockUnit == 7 ? "分钟" : (c.StockUnit == 8 ? "周" : (c.StockUnit == 9 ? "月" : "")))))))),
                Children = GetMaterialListDtos(c.MaterialId).Count() > 0? GetMaterialListDtos(c.MaterialId) : null
            }).Distinct().ToList();
            return result;
        }




    }
}
